/*
 * @Author: fishroom
 * @Date: 2021-08-22 19:31:48
 * @LastEditTime: 2021-09-04 12:19:38
 * @FilePath: \backstage\src\main.js
 */
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

Vue.config.productionTip = false;
// 轮播插件swiper全局挂载
import { Swiper as SwiperClass, Mousewheel, Autoplay } from "swiper/core";
import getAwesomeSwiper from "vue-awesome-swiper/dist/exporter";
import "swiper/swiper-bundle.css";
SwiperClass.use([Mousewheel, Autoplay]);
// 挂载到vue全局上
Vue.use(getAwesomeSwiper(SwiperClass));
// 视频插件
// import VideoPlayer from "vue-video-player";

// require("video.js/dist/video-js.css");

// require("vue-video-player/src/custom-theme.css");

// Vue.use(VideoPlayer);

import "element-ui/lib/theme-chalk/index.css";
import {
  Input,
  Button,
  Col,
  Row,
  Select,
  Option,
  Pagination,
  Carousel,
  CarouselItem,
  Avatar,
  Card,
  Drawer,
  Form,
  FormItem,
  MessageBox,
  Table,
  TableColumn,
  Switch,
  Radio,
  RadioGroup,
  Checkbox,
  CheckboxGroup,
  Descriptions,
  DescriptionsItem,
  Tag
} from "element-ui";

// 引入viser 全局挂载
import Viser from "viser-vue";
Vue.use(Viser);

import BaiduMap from "vue-baidu-map";
import axios from "axios";

Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: "百度ak"
});

Vue.use(Tag);
Vue.use(DescriptionsItem);
Vue.use(Descriptions);
Vue.use(Input);
Vue.use(Button);
Vue.use(Select);
Vue.use(Option);
Vue.use(Pagination);
Vue.use(Col);
Vue.use(Row);
Vue.use(Carousel);
Vue.use(CarouselItem);
Vue.use(Avatar);
Vue.use(Card);
Vue.use(Drawer);

Vue.use(Form);
Vue.use(FormItem);
Vue.prototype.$confirm = MessageBox.confirm;

Vue.use(Table);
Vue.use(TableColumn);

Vue.use(Switch);
Vue.use(Radio);
Vue.use(RadioGroup);

Vue.use(Checkbox);
Vue.use(CheckboxGroup);
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");

// 设置axios 的请求拦截器,在发出请求前进行操作
axios.interceptors.request.use(
  config => {
    if (localStorage.getItem("token")) {
      config.headers.Authorization = localStorage.getItem("token");
      console.log("成功");
    }
    return config;
  },
  err => {
    return Promise.promise.reject(error);
  }
);
// 设置axios的响应拦截器，在响应后的数据进行操作，即处理token失效等问题
// 请求成功，已经到达200，将执行response，请求不成功，在200之后执行error
axios.interceptors.response.use(
  response => {
    return response;
  },
  function(error) {
    console.log(error.response.data);
    let data = error.response.data;
    switch (error.response.status) {
      case 403:
        Toast({
          massage: "拒绝访问",
          position: "top"
          // duration:2000
        });
        break;
      case 500:
        if (data.message === "Token失效, 请重新登录") {
          Toast({
            message: "登录已过期,请重新登录",
            position: "top"
            // duration:2000
          });
        }
        break;
      case 404:
        Toast({
          message: "很抱歉,资源未找到",
          position: "top"
          // duration:2000
        });
        break;
      case 504:
        Toast({
          message: "网络超时",
          position: "top"
          // duration:2000
        });
        break;
      case 401:
        Toast({
          message: "未授权,请重新登录",
          position: "top"
          // duration:2000
        });
    }
    return Promise.reject(error);
  }
);

// // 导入格式化时间的插件
// import moment from "moment";
// // 定义全局的过滤器
// Vue.filter('dateFormat',function(daraStr,pattern = "YYYY-MM-DD HH:mm"){
//   return moment(dataStr).format(pattern)
// })